package co.yixiang.hw.rest;

import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.aop.log.Log;
import co.yixiang.hw.domain.SupplierGoodsSku;
import co.yixiang.hw.domain.SupplierGoodsSkuLog;
import co.yixiang.hw.domain.SupplierUserApply;
import co.yixiang.hw.service.*;
import co.yixiang.hw.service.dto.*;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.file.Path;
import java.util.List;
import java.util.Map;

/**
* @author Walert
* @date 2020-02-09
*/
@Api(tags = "hyUser管理")
@RestController
@RequestMapping("/api/supplier/sku")
public class SupplierSkuController extends BaseController {


    private final SupplierUserApplyService supplierUserApplyService;
    private final SupplierUserService supplierUserService;
    private final SupplierGoodsSkuService supplierGoodsSkuService;
    private final SupplierGoodsSkuLogService supplierGoodsSkuLogService;


    public SupplierSkuController(SupplierUserApplyService supplierUserApplyService, SupplierUserService supplierUserService,
                                 SupplierGoodsSkuService supplierGoodsSkuService,SupplierGoodsSkuLogService supplierGoodsSkuLogService) {
        this.supplierUserApplyService = supplierUserApplyService;
        this.supplierUserService = supplierUserService;
        this.supplierGoodsSkuService = supplierGoodsSkuService;
        this.supplierGoodsSkuLogService = supplierGoodsSkuLogService;
    }

    @Log("首页sku列表")
    @ApiOperation(value = "首页sku列表",response = SupplierGoodsSkuDto.class)
    @PostMapping(value = "/index")
    @AnonymousAccess
    public ResponseEntity<Object> indexSkuList(@RequestBody JSONObject params){
        Integer userLevel = this.getLevel();
        Integer type = params.getInteger("type");
        Integer pageNo = params.getInteger("pageNo");
        Integer pageSize = params.getInteger("pageSize");
        Map<String, Object> result = supplierGoodsSkuService.getGoodsSkuListByTypePage(type, userLevel, pageNo, pageSize);
        return new ResponseEntity<>(result,HttpStatus.OK);
    }



    @Log("获取sku详情")
    @ApiOperation(value = "获取sku详情",response = SupplierGoodsSkuDto.class)
    @PostMapping(value = "/details")
    @AnonymousAccess
    public ResponseEntity<Object> detailById(@RequestBody JSONObject params){
        Integer userLevel = this.getLevel();
        Long skuId = params.getLong("skuId");
        return new ResponseEntity<>(supplierGoodsSkuService.findById(userLevel,skuId),HttpStatus.OK);
    }

    private Integer getLevel() {
        Long userId = getUserId();
        Integer userLevel = 0;
        if (userId != -1L) {
            userLevel = supplierUserService.getSupplierUserLevel(userId);
        }
        return userLevel;
    }

    @Log("获取sku价格走势详情")
    @ApiOperation(value = "获取sku价格走势详情")
    @PostMapping(value = "/prices")
    @AnonymousAccess
    public ResponseEntity<Object> prices(@RequestBody JSONObject params){
        Integer userLevel = this.getLevel();
        Long skuId = params.getLong("skuId");
        return new ResponseEntity<>(supplierGoodsSkuLogService.getSkuDaysPrice(skuId,userLevel),HttpStatus.OK);
    }
}
